//
//  LJZFPlayerControlView.m
//  LajinLive
//
//  Created by 刘洋 on 2016/10/25.
//  Copyright © 2016年 lajin. All rights reserved.
//

#import "LJZFPlayerControlView.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import "UIView+CustomControlView.h"
#import "MMMaterialDesignSpinner.h"
#import "LJVideoNetStatusView.h"
#import "ASValueTrackingSlider.h"

static const CGFloat ZFPlayerAnimationTimeInterval1             = 2.0f;
static const CGFloat ZFPlayerAnimationTimeInterval2             = 5.0f;
static const CGFloat ZFPlayerControlBarAutoFadeOutTimeInterval = 0.35f;

@interface LJZFPlayerControlView () <UIGestureRecognizerDelegate>
/** 当前播放时长label */
@property (nonatomic, strong) UILabel                 *currentTimeLabel;
/** 视频总时长label */
@property (nonatomic, strong) UILabel                 *totalTimeLabel;
/** 缓冲进度条 */
@property (nonatomic, strong) UIProgressView          *progressView;
/** 滑杆 */
@property (nonatomic, strong) UISlider                *videoSlider;
/** 全屏按钮 */
@property (nonatomic, strong) UIButton                *fullScreenBtn;
/** 快进快退label */
@property (nonatomic, strong) UILabel                 *horizontalLabel;
/** 系统菊花 */
@property (nonatomic, strong) MMMaterialDesignSpinner *activity;
/** bottomView*/
@property (nonatomic, strong) UIImageView             *bottomImageView;
/** topView */
@property (nonatomic, strong) UIImageView             *topImageView;
/** 加载失败按钮 */
@property (nonatomic, strong) UIButton                *failBtn;
/** 显示控制层 */
@property (nonatomic, assign, getter=isShowing) BOOL  showing;
/** 在cell上播放 */
@property (nonatomic, assign, getter=isCellVideo) BOOL cellVideo;
/** 是否拖拽slider控制播放进度 */
@property (nonatomic, assign, getter=isDragged) BOOL  dragged;
/** 网络状态控制视图 */
@property (nonatomic, strong) LJVideoNetStatusView    *netStatusView;
/** 播放按钮 */
@property (nonatomic, strong) UIButton                *playeBtn;
/** 开始播放按钮 */
@property (nonatomic, strong) UIButton                *startBtn;

@end

@implementation LJZFPlayerControlView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createUI];
        
        // 添加子控件的约束
        [self makeSubViewsConstraints];
        [self.activity stopAnimating];

        self.failBtn.hidden         = YES;
        self.startBtn.hidden        = YES;
        self.netStatusView.hidden   = YES;
        // 初始化时重置controlView
        [self zf_playerResetControlView];
        
        // 全屏按钮
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFullScreenUpdateUI:) name:KVideoFullScreenNotification object:nil];
    }
    return self;
}

- (void)createUI
{
    [self addSubview:self.topImageView];
    [self addSubview:self.bottomImageView];
    [self.bottomImageView addSubview:self.startBtn];
    [self.bottomImageView addSubview:self.currentTimeLabel];
    [self.bottomImageView addSubview:self.progressView];
    [self.bottomImageView addSubview:self.videoSlider];
    [self.bottomImageView addSubview:self.totalTimeLabel];
    
    [self addSubview:self.fullScreenBtn];
    [self addSubview:self.activity];
    [self addSubview:self.horizontalLabel];
    [self addSubview:self.playeBtn];
    [self addSubview:self.failBtn];
    [self addSubview:self.netStatusView];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)makeSubViewsConstraints
{
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(80);
    }];
    
    [self.netStatusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    /** bottomView ==begin== */
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    [self.currentTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.bottom.mas_equalTo(-9);
        make.width.mas_equalTo(54);
        make.height.mas_equalTo(20);
    }];
    
    [self.startBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.width.height.mas_equalTo(32);
        make.centerY.mas_equalTo(self.currentTimeLabel.mas_centerY);
    }];
    
    [self.fullScreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-8);
        make.width.height.mas_equalTo(32);
        make.bottom.mas_equalTo(-5);
    }];
    
    [self.totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-40);
        make.centerY.mas_equalTo(self.currentTimeLabel.mas_centerY);
        make.width.mas_equalTo(54);
        make.height.mas_equalTo(20);
    }];
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.currentTimeLabel.mas_right).mas_offset(4);
        make.right.mas_equalTo(self.totalTimeLabel.mas_left).mas_offset(-4);
        make.centerY.mas_equalTo(self.currentTimeLabel.mas_centerY);
    }];
    
    [self.videoSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.currentTimeLabel.mas_right).mas_offset(4);
        make.right.mas_equalTo(self.totalTimeLabel.mas_left).mas_offset(-4);
        make.centerY.mas_equalTo(self.currentTimeLabel.mas_centerY).mas_offset(-1);
        make.height.mas_equalTo(30);
    }];
    /** bottomView ==begin== */
    
    [self.horizontalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(33);
        make.center.equalTo(self);
    }];
    
    [self.activity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.with.height.mas_equalTo(22);
    }];
    
    [self.playeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [self.failBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(33);
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self zf_playerCancelAutoFadeOutControlView];
    // 只要屏幕旋转就显示控制层
    [self zf_playerShowControlView];
}

#pragma mark - Action

/** UISlider TapAction */
- (void)tapSliderAction:(UITapGestureRecognizer *)tap
{
    if ([tap.view isKindOfClass:[UISlider class]]) {
        UISlider *slider = (UISlider *)tap.view;
        CGPoint point = [tap locationInView:slider];
        CGFloat length = slider.frame.size.width;
        // 视频跳转的value
        CGFloat tapValue = point.x / length;
        if ([self.delegate respondsToSelector:@selector(zf_controlView:progressSliderTap:)]) {
            [self.delegate zf_controlView:self progressSliderTap:tapValue];
        }
    }
}

// 不做处理，只是为了滑动slider其他地方不响应其他手势
- (void)panRecognizer:(UIPanGestureRecognizer *)sender {}

- (void)playBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if ([self.delegate respondsToSelector:@selector(zf_controlView:playAction:)]) {
        [self.delegate zf_controlView:self playAction:sender];
    }
}

- (void)fullScreenBtnClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(zf_controlView:fullScreenAction:)]) {
        [self.delegate zf_controlView:self fullScreenAction:sender];
    }
}

- (void)failBtnClick:(UIButton *)sender
{
    self.failBtn.hidden = YES;
    if ([self.delegate respondsToSelector:@selector(zf_controlView:failAction:)]) {
        [self.delegate zf_controlView:self failAction:sender];
    }
}

- (void)progressSliderTouchBegan:(ASValueTrackingSlider *)sender
{
    [self zf_playerCancelAutoFadeOutControlView];
    if ([self.delegate respondsToSelector:@selector(zf_controlView:progressSliderTouchBegan:)]) {
        [self.delegate zf_controlView:self progressSliderTouchBegan:sender];
    }
}

- (void)progressSliderValueChanged:(ASValueTrackingSlider *)sender
{
    if ([self.delegate respondsToSelector:@selector(zf_controlView:progressSliderValueChanged:)]) {
        [self.delegate zf_controlView:self progressSliderValueChanged:sender];
    }
}

- (void)progressSliderTouchEnded:(ASValueTrackingSlider *)sender
{
    if ([self.delegate respondsToSelector:@selector(zf_controlView:progressSliderTouchEnded:)]) {
        [self.delegate zf_controlView:self progressSliderTouchEnded:sender];
    }
}

- (void)playerPlayDidEnd
{
    self.backgroundColor  = RGBA(0, 0, 0, .6);
    // 初始化显示controlView为YES
    self.showing = NO;
    // 延迟隐藏controlView
    [self zf_playerShowControlView];
}

- (void)zf_playerEnableFullScreenBtn:(BOOL)flag
{
    self.fullScreenBtn.enabled = YES;
}

#pragma mark - Private Method

/** 重置ControlView */
- (void)zf_playerResetControlView
{
    self.videoSlider.value      = 0;
    self.progressView.progress  = 0;
    self.currentTimeLabel.text  = @"00:00";
    self.totalTimeLabel.text    = @"00:00";
    self.playeBtn.hidden        = YES;
    self.horizontalLabel.hidden = YES;
    self.backgroundColor        = [UIColor clearColor];
    self.showing                = NO;
}

- (void)showControlView
{
    self.topImageView.alpha    = 1;
    self.bottomImageView.alpha = 1;
    self.playeBtn.alpha        = 1;
    self.fullScreenBtn.alpha   = 1;
}

- (void)hideControlView
{
    if (self.startBtn.hidden) {
        self.topImageView.alpha     = 0;
        self.bottomImageView.alpha  = 0;
        self.playeBtn.alpha         = 0.5;
    } else {
        self.topImageView.alpha     = 0.5;
        self.bottomImageView.alpha  = 0.5;
        self.playeBtn.alpha         = 0.5;
        self.fullScreenBtn.alpha    = 0.5;
    }
}

#pragma mark - getter

- (UIImageView *)topImageView
{
    if (!_topImageView) {
        _topImageView                        = [[UIImageView alloc] init];
        _topImageView.userInteractionEnabled = YES;
        _topImageView.image                  = ZFPlayerImage(@"ZFPlayer_top_shadow");
    }
    return _topImageView;
}

- (LJVideoNetStatusView *)netStatusView
{
    if (!_netStatusView) {
        _netStatusView = [[LJVideoNetStatusView alloc] init];
        _netStatusView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        _netStatusView.userInteractionEnabled = YES;
    }
    return _netStatusView;
}

- (UIImageView *)bottomImageView
{
    if (!_bottomImageView) {
        _bottomImageView                        = [[UIImageView alloc] init];
        _bottomImageView.userInteractionEnabled = YES;
        _bottomImageView.image                  = ZFPlayerImage(@"ZFPlayer_bottom_shadow");
        __weak typeof(self) weakSelf = self;
        [_bottomImageView setTapActionWithBlock:^{
            [weakSelf showControlView];
        }];
    }
    return _bottomImageView;
}

- (UIButton *)startBtn
{
    if (!_startBtn) {
        _startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startBtn setImage:ZFPlayerImage(@"ZFPlayer_play") forState:UIControlStateNormal];
        [_startBtn setImage:ZFPlayerImage(@"ZFPlayer_pause") forState:UIControlStateSelected];
        [_startBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startBtn;
}

- (UILabel *)currentTimeLabel
{
    if (!_currentTimeLabel) {
        _currentTimeLabel               = [[UILabel alloc] init];
        _currentTimeLabel.textColor     = [UIColor whiteColor];
        _currentTimeLabel.font          = [UIFont systemFontOfSize:12.0f];
        _currentTimeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _currentTimeLabel;
}

- (UIProgressView *)progressView
{
    if (!_progressView) {
        _progressView                   = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.progressTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
        _progressView.trackTintColor    = [UIColor clearColor];
    }
    return _progressView;
}

- (UISlider *)videoSlider
{
    if (!_videoSlider) {
        _videoSlider                       = [[UISlider alloc] init];
        
        [_videoSlider setThumbImage:ZFPlayerImage(@"ZFPlayer_slider") forState:UIControlStateNormal];
        _videoSlider.maximumValue          = 1;
        _videoSlider.minimumTrackTintColor = [UIColor whiteColor];
        _videoSlider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
        
        // slider开始滑动事件
        [_videoSlider addTarget:self action:@selector(progressSliderTouchBegan:) forControlEvents:UIControlEventTouchDown];
        // slider滑动中事件
        [_videoSlider addTarget:self action:@selector(progressSliderValueChanged:) forControlEvents:UIControlEventValueChanged];
        // slider结束滑动事件
        [_videoSlider addTarget:self action:@selector(progressSliderTouchEnded:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchCancel | UIControlEventTouchUpOutside];
        
        UITapGestureRecognizer *sliderTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSliderAction:)];
        [_videoSlider addGestureRecognizer:sliderTap];
        
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panRecognizer:)];
        panRecognizer.delegate = self;
        [panRecognizer setMaximumNumberOfTouches:1];
        [panRecognizer setDelaysTouchesBegan:YES];
        [panRecognizer setDelaysTouchesEnded:YES];
        [panRecognizer setCancelsTouchesInView:YES];
        [_videoSlider addGestureRecognizer:panRecognizer];
    }
    return _videoSlider;
}

- (UILabel *)totalTimeLabel
{
    if (!_totalTimeLabel) {
        _totalTimeLabel               = [[UILabel alloc] init];
        _totalTimeLabel.textColor     = [UIColor whiteColor];
        _totalTimeLabel.font          = [UIFont systemFontOfSize:12.0f];
        _totalTimeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _totalTimeLabel;
}

- (UIButton *)fullScreenBtn
{
    if (!_fullScreenBtn) {
        _fullScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _fullScreenBtn.enabled = NO;
        [_fullScreenBtn setImage:ZFPlayerImage(@"ZFPlayer_fullscreen") forState:UIControlStateNormal];
        [_fullScreenBtn addTarget:self action:@selector(fullScreenBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fullScreenBtn;
}

- (UILabel *)horizontalLabel
{
    if (!_horizontalLabel) {
        _horizontalLabel                 = [[UILabel alloc] init];
        _horizontalLabel.layer.masksToBounds = YES;
        _horizontalLabel.layer.cornerRadius = 15;
        _horizontalLabel.textColor       = [UIColor whiteColor];
        _horizontalLabel.textAlignment   = NSTextAlignmentCenter;
        _horizontalLabel.font            = [UIFont systemFontOfSize:15.0];
        _horizontalLabel.backgroundColor = [UIColor colorWithPatternImage:ZFPlayerImage(@"ZFPlayer_management_mask")];
    }
    return _horizontalLabel;
}

- (MMMaterialDesignSpinner *)activity
{
    if (!_activity) {
        _activity = [[MMMaterialDesignSpinner alloc] init];
        _activity.tintColor = [UIColor whiteColor];
    }
    return _activity;
}

- (UIButton *)playeBtn
{
    if (!_playeBtn) {
        _playeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playeBtn setImage:ZFPlayerImage(@"ZFPlayer_play_btn_normal") forState:UIControlStateNormal];
        [_playeBtn setImage:ZFPlayerImage(@"ZFPlayer_play_btn_high") forState:UIControlStateHighlighted];
        _playeBtn.userInteractionEnabled = NO;
    }
    return _playeBtn;
}

- (UIButton *)failBtn
{
    if (!_failBtn) {
        _failBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_failBtn setTitle:NSLocalizedString(@"Loading Failed", nil) forState:UIControlStateNormal];
        [_failBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _failBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        _failBtn.backgroundColor = RGBA(0, 0, 0, 0.7);
        [_failBtn addTarget:self action:@selector(failBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _failBtn;
}

- (void)autoFadeOutControlView
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(zf_playerHideControlView) object:nil];
    CGFloat timeInterval = ZFPlayerAnimationTimeInterval1;
    if (self.startBtn.hidden) {//非全屏
        timeInterval = ZFPlayerAnimationTimeInterval2;
    }
    [self performSelector:@selector(zf_playerHideControlView) withObject:nil afterDelay:timeInterval];
}

/**
 slider滑块的bounds
 */
- (CGRect)thumbRect
{
    return [self.videoSlider thumbRectForBounds:self.videoSlider.bounds
                                      trackRect:[self.videoSlider trackRectForBounds:self.videoSlider.bounds]
                                          value:self.videoSlider.value];
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGRect rect = [self thumbRect];
    CGPoint point = [touch locationInView:self.videoSlider];
    if ([touch.view isKindOfClass:[UISlider class]]) { // 如果在滑块上点击就不响应pan手势
        if (point.x <= rect.origin.x + rect.size.width && point.x >= rect.origin.x) { return NO; }
    }
    return YES;
}

#pragma mark - Public method
/**
 *  取消延时隐藏controlView的方法
 */
- (void)zf_playerCancelAutoFadeOutControlView
{
    self.showing = NO;
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoFadeOutControlView) object:nil];
}

/**
 *  显示控制层
 */
- (void)zf_playerShowControlView
{
    if (self.isShowing) {
        [self zf_playerHideControlView];
        return;
    }
    [self zf_playerCancelAutoFadeOutControlView];
    [UIView animateWithDuration:ZFPlayerControlBarAutoFadeOutTimeInterval animations:^{
        [self showControlView];
    } completion:^(BOOL finished) {
        self.showing = YES;
        [self autoFadeOutControlView];
    }];
}

/**
 *  隐藏控制层
 */
- (void)zf_playerHideControlView
{
    if (!self.isShowing) { return; }
    [self zf_playerCancelAutoFadeOutControlView];
    [UIView animateWithDuration:ZFPlayerControlBarAutoFadeOutTimeInterval animations:^{
        [self hideControlView];
    }completion:^(BOOL finished) {
        self.showing = NO;
    }];
}

/** 在cell播放 */
- (void)zf_playerCellPlay
{
    self.cellVideo = YES;
    [self layoutIfNeeded];
    [self zf_playerShowControlView];
}

- (void)zf_playerCurrentTime:(NSInteger)currentTime totalTime:(NSInteger)totalTime sliderValue:(CGFloat)value
{
    // 当前时长进度progress
    NSInteger proHour          = currentTime / 3600;//当前小时
    NSInteger proMin           = (currentTime - proHour * 3600) / 60;//当前分钟
    NSInteger proSec           = currentTime % 60;//当前秒
    
    // duration 总时长
    NSInteger durHour          = totalTime / 3600;//当前小时
    NSInteger durMin           = (totalTime - durHour * 3600) / 60;//总分钟
    NSInteger durSec           = totalTime % 60;//总秒
    
    if (!self.isDragged) {
        // 更新slider
        self.videoSlider.value = value;
    }
    
    if (proHour != 0) {
        // 更新当前播放时间
        self.currentTimeLabel.text = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", proHour, proMin, proSec];
    } else {
        if (durHour != 0) {
            // 更新当前播放时间
            self.currentTimeLabel.text = [NSString stringWithFormat:@"00:%02zd:%02zd", proMin, proSec];
        } else {
            // 更新当前播放时间
            self.currentTimeLabel.text = [NSString stringWithFormat:@"%02zd:%02zd", proMin, proSec];
        }
    }
    
    if (durHour != 0) {
        // 更新总时间
        self.totalTimeLabel.text   = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", durHour, durMin, durSec];
    } else {
        // 更新总时间
        self.totalTimeLabel.text   = [NSString stringWithFormat:@"%02zd:%02zd", durMin, durSec];
    }
}

- (void)zf_playerDraggedTime:(NSInteger)draggedTime totalTime:(NSInteger)totalTime isForward:(BOOL)forawrd hasPreview:(BOOL)preview
{
    // 当前时长进度progress
    NSInteger proHour          = draggedTime / 3600;//当前小时
    NSInteger proMin           = (draggedTime - proHour * 3600) / 60;//当前分钟
    NSInteger proSec           = draggedTime % 60;//当前秒
    
    // duration 总时长
    NSInteger durHour          = totalTime / 3600;//当前小时
    NSInteger durMin           = (totalTime - durHour * 3600) / 60;//总分钟
    NSInteger durSec           = totalTime % 60;//总秒
    
    NSString *currentTimeStr = [NSString stringWithFormat:@"%02zd:%02zd", proMin, proSec];
    NSString *totalTimeStr   = [NSString stringWithFormat:@"%02zd:%02zd", durMin, durSec];
    if (proHour != 0) {
        // 更新当前播放时间
        currentTimeStr = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", proHour, proMin, proSec];
    }
    
    if (durHour != 0) {
        // 更新总时间
        totalTimeStr = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", durHour, durMin, durSec];
    }
    
    // 更新slider的值
    self.videoSlider.value = (CGFloat)draggedTime/(CGFloat)totalTime;
    // 更新当前时间
    self.currentTimeLabel.text = currentTimeStr;
    // 正在拖动控制播放进度
    self.dragged = YES;
    
    NSString *style;
    if (forawrd) { style = @">>"; } else { style = @"<<"; }
    
    self.horizontalLabel.hidden = preview;
    self.horizontalLabel.text = [NSString stringWithFormat:@"%@ %@ / %@", style, currentTimeStr, totalTimeStr];
}

- (void)zf_playerDraggedEnd
{
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        weakSelf.horizontalLabel.hidden = YES;
    });
    self.dragged = NO;
    // 结束滑动时候把开始播放按钮改为播放状态
    self.startBtn.selected = YES;
    [self showPlayeBtn:YES];
    // 滑动结束延时隐藏controlView
    [self autoFadeOutControlView];
}

/** progress显示缓冲进度 */
- (void)zf_playerSetProgress:(CGFloat)progress
{
    [self.progressView setProgress:progress animated:NO];
}

/** 视频加载失败 */
- (void)zf_playerItemStatusFailed:(NSError *)error
{
    self.failBtn.hidden = NO;
}

/** 加载的菊花 */
- (void)zf_playerActivity:(BOOL)animated
{
    if (animated) {
        self.activity.hidden = NO;
        [self.activity startAnimating];
        self.horizontalLabel.hidden = YES;
    } else {
        [self.activity stopAnimating];
        self.activity.hidden = YES;
    }
}

/** 播放按钮状态 */
- (void)zf_playerPlayBtnState:(BOOL)state
{
    self.startBtn.selected = state;
    [self showPlayeBtn:state];
}

/** 网络视图按钮 */
- (void)zf_playerNetStatusViewHidden:(BOOL)flag{
    self.netStatusView.hidden = flag;
};

/**
 控制非全屏状态下的暂停的显示与隐藏
 */
- (void)zf_playerPlayeBtnHidden:(BOOL)flag{
    self.playeBtn.hidden = flag;
};

- (void)showPlayeBtn:(BOOL)flag
{
    _playeBtn.selected = flag;
    if (!_playeBtn.selected && _startBtn.hidden) {
        _playeBtn.hidden = NO;
    } else {
        _playeBtn.hidden = YES;
    }
}

/** 全屏按钮按下更新界面UI */
- (void)videoFullScreenUpdateUI:(NSNotification *)notification
{
    NSDictionary *dic = notification.object;
    NSNumber *isFullScreen = [dic objectForKey:KVideoFullScreenNotification];
    BOOL flag = [isFullScreen boolValue];
    if (!flag) {//全屏
        _startBtn.hidden = NO;
        _playeBtn.hidden = YES;
        _fullScreenBtn.hidden = YES;
        
        [self.currentTimeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(40);
            make.bottom.mas_equalTo(-9);
            make.width.mas_equalTo(54);
            make.height.mas_equalTo(20);
        }];
        
        [self.totalTimeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-20);
            make.centerY.mas_equalTo(self.currentTimeLabel.mas_centerY);
            make.width.mas_equalTo(54);
        }];
    } else {//退出全屏
        _startBtn.hidden = YES;
        [self showPlayeBtn:_startBtn.selected];
        _fullScreenBtn.hidden = NO;
        
        [self.currentTimeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.bottom.mas_equalTo(-9);
            make.width.mas_equalTo(54);
            make.height.mas_equalTo(20);
        }];
        
        [self.fullScreenBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-8);
            make.width.height.mas_equalTo(32);
            make.centerY.mas_equalTo(self.currentTimeLabel.mas_centerY);
        }];
        
        [self.totalTimeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-40);
            make.centerY.mas_equalTo(self.currentTimeLabel.mas_centerY);
            make.width.mas_equalTo(54);
            make.height.mas_equalTo(20);
        }];
    }
}

- (void)setVideoScreenRatio:(VideoScreenRatio)videoScreenRatio
{
    if (_videoScreenRatio == videoScreenRatio) {
        return;
    }
    _videoScreenRatio = videoScreenRatio;
    
    if (_videoScreenRatio == VideoScreenRatioWEH) {//1:1隐藏全屏按钮
        _fullScreenBtn.hidden = YES;
        [self.totalTimeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-8);
            make.centerY.mas_equalTo(self.currentTimeLabel.mas_centerY);
            make.width.mas_equalTo(54);
            make.height.mas_equalTo(20);
        }];
    }
}

- (void)netStatusViewTips:(NSString *)tips btnTitle:(NSString *)title btnBlock:(void(^)())btnblock {
    self.netStatusView.netStatusTips = tips;
    self.netStatusView.netStatusBtnTitle = title;
    self.netStatusView.btnActionBlock = btnblock;
}

@end
